package com.ylbh.modules.shoptemplate.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylbh.common.pojo.shoptemplate.dto.CommodityBankDTO;
import com.ylbh.common.pojo.shoptemplate.vo.CommodityBankVO;
import com.ylbh.common.pojo.shoptemplate.vo.ShopClassVO;
import com.ylbh.common.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ylbh.modules.shoptemplate.mapper.ShoppingShopTemplateMapper;
import com.ylbh.modules.shoptemplate.entity.ShoppingShopTemplate;
import com.ylbh.modules.shoptemplate.service.ShoppingShopTemplateService;


@Service("shoppingShopTemplateService")
public class ShoppingShopTemplateServiceImpl extends ServiceImpl<ShoppingShopTemplateMapper, ShoppingShopTemplate> implements ShoppingShopTemplateService {

    @Autowired
    private ShoppingShopTemplateMapper shoppingShopTemplateMapper;

    @Value("${url.shopTemplate}")
    private String shopTemplate;

    /**
     * 店铺模板列表
     * @param pageDTO
     * @return
     */
    @Override
    public PageUtils queryShopTemplateList(PageDTO pageDTO) {
        Page<Integer> page = new Page<>(pageDTO.getPage(), pageDTO.getLimit());
        List<ShoppingShopTemplate> list = shoppingShopTemplateMapper.selectShopTemplateList(pageDTO.getKeywords(), page);
        return new PageUtils(new Page<ShoppingShopTemplate>().setRecords(list).setTotal(page.getTotal()));
    }

    /**
     * 删除店铺模板
     * @param ids
     */
    @Override
    public void removeShopTemplateById(List<Long> ids) {
        shoppingShopTemplateMapper.updateShopTemplateById(ids);
    }

    /**
     * 获取店铺模板下的分类列表
     * @param pageDTO
     * @return
     */
    @Override
    public PageUtils getShopClassListById(PageDTO pageDTO) {
        Page<Integer> page = new Page<>(pageDTO.getPage(), pageDTO.getLimit());
        List<ShopClassVO> list = shoppingShopTemplateMapper.selectShopClassListById(pageDTO.getKeywords(), page);
        return new PageUtils(new Page<ShopClassVO>().setRecords(list).setTotal(page.getTotal()));
    }

    /**
     * 获取店铺模板下的分类详情
     * @param id
     * @return
     */
    @Override
    public ShopClassVO getShopClassDetailsById(Long id) {
        return shoppingShopTemplateMapper.selectShopClassDetailsById(id);
    }

    /**
     * 修改店铺模板下的分类详情
     * @param shopClassVO
     */
    @Override
    public void modifyShopClassDetailsById(ShopClassVO shopClassVO) {
        shopClassVO.setUserId(ShiroUtils.getUserId());
        shoppingShopTemplateMapper.updateShopClassDetailsById(shopClassVO);
    }

    /**
     * 新增店铺模板下的分类
     * @param shopClassVO
     */
    @Override
    public void addShopClass(ShopClassVO shopClassVO) {
        shopClassVO.setId(new IdWorker().nextId());
        shopClassVO.setUserId(ShiroUtils.getUserId());
        shoppingShopTemplateMapper.insertShopClass(shopClassVO);
    }

    /**
     * 删除店铺模板下的分类
     * @param ids
     */
    @Override
    public void removeShopClassById(List<Long> ids) {
        shoppingShopTemplateMapper.updateShopClassById(ids);
        // 删除店铺和商品库的中间表数据
        for (Long id:ids) {
            shoppingShopTemplateMapper.deleteShopGoodsById(id);
        }
    }

    /**
     * 新增店铺模板
     * @param shoppingShopTemplate
     */
    @Override
    public void addShopTemplate(ShoppingShopTemplate shoppingShopTemplate) {
        shoppingShopTemplate.setCreateTime(new Date());
        shoppingShopTemplate.setCreateUserId(ShiroUtils.getUserId().toString());
        if (!StringUtils.isEmpty(shoppingShopTemplate.getDescribe())) {
            String url = UploadHtmlFile.uploadHtmlOss(shoppingShopTemplate.getDescribe(), shopTemplate);
            shoppingShopTemplate.setShopTemplateUrl("https://file.yilianbaihui.cn/" + shopTemplate + "/" + url);
        }
        shoppingShopTemplateMapper.insertShopTemplate(shoppingShopTemplate);
    }

    /**
     * 店铺模板详情
     * @param id
     * @return
     */
    @Override
    public ShoppingShopTemplate getShopTemplateInfoById(Long id) {
        return shoppingShopTemplateMapper.selectShopTemplateInfoById(id);
    }

    /**
     * 修改店铺模板
     * @param shoppingShopTemplate
     */
    @Override
    public void modifyShopTemplateById(ShoppingShopTemplate shoppingShopTemplate) {
        if (!StringUtils.isEmpty(shoppingShopTemplate.getDescribe())) {
            String url = UploadHtmlFile.uploadHtmlOss(shoppingShopTemplate.getDescribe(), shopTemplate);
            shoppingShopTemplate.setShopTemplateUrl("https://file.yilianbaihui.cn/" + shopTemplate + "/" + url);
        }
        shoppingShopTemplateMapper.updateShopTemplateDetailsById(shoppingShopTemplate);
    }

    /**
     * 商品库商品
     * @param commodityBankDTO
     * @return
     */
    @Override
    public PageUtils queryCommodityBank(CommodityBankDTO commodityBankDTO) {
        Page<Integer> page = new Page<>(commodityBankDTO.getPage(), commodityBankDTO.getLimit());
        List<CommodityBankVO> list = shoppingShopTemplateMapper.selectCommodityBank(commodityBankDTO, page);
        return new PageUtils(new Page<CommodityBankVO>().setTotal(page.getTotal()).setRecords(list));
    }

    /**
     * 添加商品到模板分类
     * @param id
     * @param goodsIds
     */
    @Override
    public void addShopClassGoods(String id, List<String> goodsIds) {
        for (String goodsId:goodsIds) {
            shoppingShopTemplateMapper.insertShopClassGoods(id, goodsId);
        }
    }

    /**
     * 店铺商品列表
     * @param commodityBankDTO
     * @return
     */
    @Override
    public PageUtils queryShopClassGoodsList(CommodityBankDTO commodityBankDTO) {
        Page<Integer> page = new Page<>(commodityBankDTO.getPage(), commodityBankDTO.getLimit());
        List<CommodityBankVO> list = shoppingShopTemplateMapper.selectShopClassGoodsList(commodityBankDTO.getId(), page);
        return new PageUtils(new Page<CommodityBankVO>().setRecords(list).setTotal(page.getTotal()));
    }

    /**
     * 删除分类下商品
     * @param id
     * @param goodsIds
     */
    @Override
    public void removeShopClassGoodsByShopIdAndGoodsId(String id, List<String> goodsIds) {
        shoppingShopTemplateMapper.deleteShopClassGoodsByShopIdAndGoodsId(id, goodsIds);
    }

    /**
     * 店铺模板启用
     * @param id
     * @param type
     */
    @Override
    public void modifyShopTemplateStateById(String id, Integer type) {
        shoppingShopTemplateMapper.updateShopTemplateStateById(id, type);
    }
}
